package com.chenglu.android.payment.pay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.MainActivity;
import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseActionBarActivity;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.BaseDialog;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.mine.bean.AddressListBean;
import com.chenglu.android.order.OrderActivity;
import com.chenglu.android.payment.PayUtils;
import com.chenglu.android.payment.adapter.OrderPaytypeAdapter;
import com.chenglu.android.payment.bean.ComfirmOrderBean;
import com.chenglu.android.payment.bean.CouponListBean;
import com.chenglu.android.payment.bean.PayOrderBean;
import com.chenglu.android.payment.bean.PayTypeConfig;
import com.chenglu.android.payment.bean.PostDayBean;
import com.chenglu.android.payment.bean.PostTimeListBean;
import com.chenglu.android.payment.bean.TenantIdPayMethod;
import com.chenglu.android.payment.mvp.contract.ComfirmOrderContract;
import com.chenglu.android.payment.mvp.presenter.ComfirmOrderPresenter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023/10/13 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：支付成功的
 */
public class PayStatusActivity extends BaseActionBarActivity<ComfirmOrderPresenter> implements ComfirmOrderContract.View {


    public static final String ORDERNO = "orderNo"; //支付的订单编号
    public static final String PAYTYPE = "payType"; //支付类型
    public static final String PAYMONEY = "payMoney"; //支付金额

    public static final String PAYSUCCESS = "paySuccess"; //支付状态

    public static final String PAYMSG = "PAYMSG";//支付的信息

    public static final String TENANTID = "tenantId";//商户id

    public static final String SHOPSTYPEID = "shopsTypeId";//商超类型

    public static final String REDBEAN = "REDBEAN";//商超类型

    @BindView(R.id.iv_supply_success)
    ImageView ivSupplySuccess;
    @BindView(R.id.tv_pay_text)
    TextView tvPayText;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_retrun_home)
    TextView tvRetrunHome;
    @BindView(R.id.tv_pay_info)
    TextView tvPayInfo;
    @BindView(R.id.tv_rmb)
    TextView tvRmb;
    @BindView(R.id.iv_bean)
    ImageView ivBean;
    @BindView(R.id.tv_buy_bean)
    TextView tvBuyBean;
    @BindView(R.id.tv_get)
    TextView tvGet;
    @BindView(R.id.tv_red_bean)
    TextView tvRedBean;
    @BindView(R.id.tv_red_bean_info)
    TextView tvRedBeanInfo;
    @BindView(R.id.ll_order_equity)
    LinearLayout llOrderEquity;
    //接收订单编号
    String orderNo;
    //支付金额
    String payMoney;
    //传递过来的类型参数
    int payType, shopsTypeId; //支付类型，商超
    //支付过来的提示 失败时使用
    String paymsg;

    String tenantId;//商户id
    //赠送的红豆
    double redBean;

    boolean isPaySuccess;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay_status;
    }

    @Override
    protected void initView() {
        mPresenter = new ComfirmOrderPresenter();
        mPresenter.attachView(this);

        orderNo = getIntent().getStringExtra(ORDERNO);
        payMoney = getIntent().getStringExtra(PAYMONEY);
        payType = getIntent().getIntExtra(PAYTYPE, 0);
        shopsTypeId = getIntent().getIntExtra(SHOPSTYPEID, 0);
        isPaySuccess = getIntent().getBooleanExtra(PAYSUCCESS, false);
        paymsg = getIntent().getStringExtra(PAYMSG);

        tenantId = getIntent().getStringExtra(TENANTID);

        redBean = getIntent().getDoubleExtra(REDBEAN, 0);


        initPayStatus();

    }


    //供应链商品
    private void initPayStatus() {

        if (isPaySuccess) {

//            tvOrderPayNumber.setText("订单编号:" + orderNumber);
            tvPayText.setText("支付成功");
            LogUtils.v("订单支付成功" + isPaySuccess);
            ivSupplySuccess.setBackground(getResources().getDrawable(R.drawable.default_icon_success));
            tvPrice.setText(Utils.formatZeroNumber(payMoney));
            tvPayInfo.setText("查看详情");
            //显示人民币字符
            tvRmb.setVisibility(View.VISIBLE);
            //赠送的红豆
            llOrderEquity.setVisibility(View.VISIBLE);
            tvRedBean.setText(redBean+"");

        } else {

            if (TextUtils.isEmpty(paymsg)) {
                tvPrice.setVisibility(View.GONE);
            } else {
                tvPrice.setText(paymsg);
                tvPrice.setVisibility(View.VISIBLE);
            }
            tvPayText.setText("支付失败");
            tvPayInfo.setText("重新支付");
            LogUtils.v("订单支付失败" + isPaySuccess);
            ivSupplySuccess.setBackground(getResources().getDrawable(R.drawable.default_icon_fail));
            tvRmb.setVisibility(View.GONE);
            llOrderEquity.setVisibility(View.GONE);
        }

    }


    @Override
    public void getComfirmOrder(ComfirmOrderBean bean) {

    }

    @Override
    public void getCouponList(CouponListBean bean) {

    }

    @Override
    public void getPayTypeConfig(PayTypeConfig bean) {

    }

    @Override
    public void getenantIdPayMethod(TenantIdPayMethod bean) {


        showpayTypePopup = new BottomPopup(this);
        showpayTypePopup.setPayMethodList(bean.getData());
        new XPopup.Builder(this)
                .moveUpToKeyboard(false)
                .enableDrag(false)
                .animationDuration(150)
                .asCustom(showpayTypePopup)
                .show();
    }

    @Override
    public void getGenerateOrder(ComfirmOrderBean bean) {

    }

    @Override
    public void getPayOrder(PayOrderBean bean) {
        //支付成功 刷新数据
        if (bean.getCode() == Constant.HTTPCODE200) {
            ToastUtils.show(bean.getMsg());
            isPaySuccess = true;
            initPayStatus();
        } else {
            ToastUtils.show(bean.getMsg());
            isPaySuccess = false;
            initPayStatus();
        }

    }

    @Override
    public void getAddressList(AddressListBean bean) {

    }

    @Override
    public void getPostDayList(PostDayBean bean) {

    }

    @Override
    public void getPostTimeList(PostTimeListBean bean) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_retrun_home, R.id.tv_pay_info})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_retrun_home:
                startActivity( new Intent(this, MainActivity.class));
                finish();
                break;
            case R.id.tv_pay_info:

                //弹出对话框

                if (tvPayInfo.getText().toString().equals("重新支付")) {

                    mPresenter.getenantIdPayMethod(getToken(), Constant.APPCLIENT, tenantId);

                } else {
                    startActivity(new Intent(this, OrderActivity.class)
                    .putExtra(OrderActivity.STATUS,"0"));
                    finish();
                }


                break;
        }
    }


    //对话框选择的参数类型
    int payTpyeWay = 0;
    private int mPayposition;
    BottomPopup showpayTypePopup;
    private OrderPaytypeAdapter orderPaytypeAdapter;

    class BottomPopup extends BottomPopupView {

        ImageView ivDialogDelete;
        Button btnPayOrder;
        RecyclerView rvPay;

        //注意：自定义弹窗本质是一个自定义View，但是只需重写一个参数的构造，其他的不要重写，所有的自定义弹窗都是这样。
        public BottomPopup(@NonNull Context context) {
            super(context);
        }

        // 返回自定义弹窗的布局
        @Override
        protected int getImplLayoutId() {
            return R.layout.dialog_pay_order;
        }

        // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
        @Override
        protected void onCreate() {
            super.onCreate();
            //创建的时候默认的金豆支付


            ivDialogDelete = findViewById(R.id.iv_dialog_delete);
            btnPayOrder = findViewById(R.id.btn_pay_order);
            rvPay = findViewById(R.id.rv_pay);

            orderPaytypeAdapter = new OrderPaytypeAdapter(mContext, R.layout.item_pay_method, payMethodList);
            rvPay.setLayoutManager(new LinearLayoutManager(mContext));
            rvPay.setAdapter(orderPaytypeAdapter);
            //默认选中第一个
            orderPaytypeAdapter.setThisPosition(0);
            orderPaytypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                    mPayposition = position;
                    orderPaytypeAdapter.setThisPosition(position);

//                comfirmOrderAdapter.getData().get(position).getOrderGoodsDTOList();
                }
            });

            btnPayOrder.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();

                    BaseDialog.showDialog(mContext, "确定使用"+orderPaytypeAdapter.getData().get(mPayposition).getDescriptions()+"支付吗", "取消", "确定", new OnConfirmListener() {
                        @Override
                        public void onConfirm() {

                            JSONObject jsonObject = PayUtils.setPayParam(orderNo, orderPaytypeAdapter.getData().get(mPayposition).getPayType(), orderPaytypeAdapter.getData().get(mPayposition).getId());

                            mPresenter.getPayOrder(getToken(), Utils.getRequestBody(jsonObject));


                        }
                    },false);


                }
            });

            ivDialogDelete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });


        }

        // 设置最大宽度，看需要而定，
        @Override
        protected int getMaxWidth() {
            return super.getMaxWidth();
        }

        @Override
        protected int getMaxHeight() {
            return super.getMaxHeight();
        }

        @Override
        protected PopupAnimator getPopupAnimator() {
            return super.getPopupAnimator();
        }

        protected int getPopupWidth() {
            return 0;
        }

        protected int getPopupHeight() {
            return 0;
        }


        private List<TenantIdPayMethod.Data> payMethodList;

        public List<TenantIdPayMethod.Data> getPayMethodList() {
            return payMethodList;
        }

        public void setPayMethodList(List<TenantIdPayMethod.Data> payMethodList) {
            this.payMethodList = payMethodList;
        }


    }
}
